package com.vanvalt.payhui.modules.pay.api.business;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.vanvalt.payhui.modules.pay.api.command.impl.BannerCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.PageCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.RequestCommand;
import com.vanvalt.payhui.modules.pay.api.command.impl.ResponseCommand;
import com.vanvalt.payhui.modules.pay.api.util.ErrorCode;
import com.vanvalt.payhui.modules.pay.entity.Banner;
import com.vanvalt.payhui.modules.pay.service.BannerService;
import com.vanvalt.payhui.modules.pay.service.UserService;
import com.vanvalt.payhui.util.constat.Constant;
import com.vanvalt.payhui.util.json.JsonConversion;

/**
 * Banner处理
 *
 */
public class BannerProcess extends BaseProcess {
	/**
	 *日志.
	 */
	private static Logger 				logger = Logger.getLogger(BannerProcess.class);
	/*private static HttpServletRequest	httpServletRequest	= ((ServletRequestAttributes) RequestContextHolder
			.getRequestAttributes()).getRequest();*/
	
	private UserService userService;
	private BannerService bannerService;
	
	/**
	 * 构造方法.
	 */	
	public BannerProcess() {
		super();
	}

	/**
	 * @param bannerService
	 */
	public BannerProcess(BannerService bannerService){
		super();
		this.bannerService = bannerService;
	}
	
	/**
	 * @param userService
	 * @param bannerService
	 */
	public BannerProcess(UserService userService, BannerService bannerService){
		super();
		this.userService = userService;
		this.bannerService = bannerService;
	}

	/**
	 * Banner图列表
	 * @param pageCommand
	 * @param requestCommand
	 * @param responseCommand
	 * @return
	 * @throws JsonParseException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	public ResponseCommand list(PageCommand pageCommand, RequestCommand requestCommand, ResponseCommand responseCommand)
			throws JsonParseException, JsonMappingException, IOException {
		
		// 分页参数
		pageCommand = initPagecommand(pageCommand, requestCommand);
		RowBounds rowBounds = new RowBounds(pageCommand.getPage().getOffset(), 
				pageCommand.getPage().getPageSize());
		
		BannerCommand bannerCommand = new BannerCommand(requestCommand.getObject().toString());
		Map<String, Object> map = bannerCommand.getMap();
		String uid = (String) map.get("uid");
        String currentPage = (String) map.get("currentPage");
        String pageSize = (String) map.get("pageSize");
		
        Map<String,Object> resultMap = Maps.newHashMap();
        
		if(map.isEmpty() && StringUtils.isBlank(uid) && StringUtils.isBlank(currentPage) && StringUtils.isBlank(pageSize)){
            return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED, responseCommand);
        } else {
        	
        	User user = userService.get(uid);
        	
        	if(user == null){
        		return returnResult(ErrorCode.ERR_CODE_STATUS_FALSE, ErrorCode.ERR_CODE_PRECONDITION_FAILED_USER_NOT_EXIST, responseCommand);
        	} else {
        		
        		List<Map<String,Object>> resultList = Lists.newArrayList();
        		
        		/*Integer currentPages = Integer.parseInt(currentPage);
                Integer lineSize = Integer.parseInt(pageSize);*/
                
                Banner bannerParams = new Banner();
                bannerParams.setType(Constant.BANNER_TYPE_MOBILE); // Banner图（手机版）
                bannerParams.setDelFlag(Constant.DEL_FLAG_NORMAL); // 正常
                bannerParams.setOffset(rowBounds.getOffset());
                bannerParams.setLimit(rowBounds.getLimit());
                
               /* Page<Banner> page = bannerService.findPage(new Page<Banner>(currentPages, lineSize, 3), bannerParams); 
                List<Banner> bannerList = page.getList();*/
         		List<Banner> bannerList = this.bannerService.findListByPage(bannerParams);
                
                String baseUrl = Global.getConfig("web.file.download.url");
                
                for(Banner banner:bannerList){
                	
                	Map<String,Object> bannerMap = Maps.newHashMap();
                	
                	bannerMap.put("title", banner.getTitle());
                	String image = banner.getImage();
                	String imgUrl = "";
                	if(image != null && !"".equals(image)){
                		imgUrl = baseUrl + image;
                	} 
                	bannerMap.put("link", banner.getLink());
                	
                	/*if(imgUrl.contains("_thumbs/")){
                		imgUrl = imgUrl.replace("_thumbs/", "");
                	}*/
                	
                	bannerMap.put("image", imgUrl);
                	
                	resultList.add(bannerMap);
                }
                
                resultMap.put("bannerList", resultList);
                
                String projectFrontUrl = Global.getConfig("web.project.front.url");
                String link = projectFrontUrl + "/pay/agent/toapply?uid="+uid;
                resultMap.put("agentApplyUrl", link);
         		
         		return returnResult(ErrorCode.ERR_CODE_STATUS_TRUE, ErrorCode.ERR_CODE_SUCCESS, 
         				JsonConversion.writeMapJSON(resultMap), responseCommand);
        	}
        }
	}

	public BannerService getBannerService() {
		return bannerService;
	}

	public void setBannerService(BannerService bannerService) {
		this.bannerService = bannerService;
	}
	
}
